package com.feituan.core.persistence.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feituan.core.persistence.mapper.CouponMapper;
import com.feituan.core.persistence.mapper.UserCouponMapper;
import com.feituan.core.persistence.service.IUserCouponService;
import com.feituan.core.pojo.entity.Coupon;
import com.feituan.core.pojo.entity.UserCoupon;
import com.feituan.core.pojo.request.CouponAndCount;
import com.feituan.core.pojo.request.UserCouponPageReq;
import com.feituan.core.pojo.request.UserCouponQuery;
import com.feituan.core.pojo.request.UserCouponSendReq;
import com.feituan.core.pojo.response.UserCouponList;
import com.feituan.core.pojo.response.page.PageData;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaohu
 * @since 2024-05-09
 */
@Service
public class UserCouponServiceImpl extends ServiceImpl<UserCouponMapper, UserCoupon> implements IUserCouponService {

    @Resource
    private CouponMapper couponMapper;

    @Override
    public PageData<UserCoupon> queryPage(UserCouponPageReq request) {
        Page<UserCoupon> page = this.lambdaQuery()
                .eq(request.getUserId() != null,UserCoupon::getUserId,request.getUserId())
                .page(new Page<>(request.getPage(), request.getLimit()));
        return PageData.build(page.getCurrent(), page.getTotal(), page.getRecords());
    }

    @Override
    public void insertBatch(List<UserCoupon> userCouponList) {
        this.getBaseMapper().insertBatch(userCouponList);
    }

    /**
     * 给对应用户发送优惠卷
     * @param userCouponSendReq userCouponSendReq
     */
    @Override
    public void sendCoupon(UserCouponSendReq userCouponSendReq) {
        List<Long> userIdList = userCouponSendReq.getUserIdList();
        // 批量赠送
        userIdList.forEach(el -> {
            List<CouponAndCount> couponAndCountList = userCouponSendReq.getCouponAndCountList();
            List<UserCoupon> userCouponList = new ArrayList<>();
            Date date = new Date();
            for (CouponAndCount couponAndCount : couponAndCountList) {
                for (int i = 0; i < couponAndCount.getTotal(); i++) {
                    UserCoupon userCoupon = new UserCoupon();
                    userCoupon.setUserId(el);
                    userCoupon.setCouponId(couponAndCount.getCouponId());
                    userCoupon.setCouponStatus(0);
                    userCoupon.setCreateTime(date);
                    userCouponList.add(userCoupon);
                }
            }
            this.getBaseMapper().insertBatch(userCouponList);
        });


    }

    @Override
    public PageData<UserCouponList> selectCouponByUserId(UserCouponQuery userCouponQuery) {
        Page<UserCoupon> userCouponPage = new Page<>();
        userCouponPage.setCurrent(userCouponQuery.getPage());
        userCouponPage.setSize(userCouponQuery.getLimit());
        LambdaQueryWrapper<UserCoupon> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserCoupon::getUserId, userCouponQuery.getUserId());
        queryWrapper.eq(userCouponQuery.getCouponStatus() != null, UserCoupon::getCouponStatus, userCouponQuery.getCouponStatus());
        // 小程序只查询未使用的优惠券
        queryWrapper.eq("true".equals(userCouponQuery.getWxQuery()), UserCoupon::getCouponStatus, 0);
        queryWrapper.eq(userCouponQuery.getCouponId() != null, UserCoupon::getCouponId, userCouponQuery.getCouponId());
        Page<UserCoupon> page = this.getBaseMapper().selectPage(userCouponPage, queryWrapper);
        if (CollectionUtils.isEmpty(page.getRecords())) {
            return PageData.build(page.getCurrent(), page.getTotal(), new ArrayList<UserCouponList>());
        }

        List<UserCouponList> userCouponListInfos = new ArrayList<>();
        List<UserCoupon> records = page.getRecords();
        records.forEach(el -> {
            UserCouponList userCoupon = new UserCouponList();
            Coupon coupon = couponMapper.selectById(el.getCouponId());
            BeanUtils.copyProperties(el, userCoupon);
            userCoupon.setCoupon(coupon);
            userCouponListInfos.add(userCoupon);
        });
        return PageData.build(page.getCurrent(), page.getTotal(), userCouponListInfos);



//        Map<Long, List<UserCoupon>> collect = page.getRecords().stream().collect(Collectors.groupingBy(UserCoupon::getCouponId));
//        List<UserCouponListInfo> userCouponListInfos = new ArrayList<>();
//        collect.forEach((couponId, userCoupons) -> {
//            Coupon coupon = couponMapper.selectById(couponId);
//            UserCouponListInfo userCouponListInfo = new UserCouponListInfo();
//            userCouponListInfo.setCoupon(coupon);
//            userCouponListInfo.setCouponId(couponId);
//            userCouponListInfo.setCouponNum(userCoupons.size());
//            userCouponListInfo.setUserCouponList(userCoupons);
//            userCouponListInfos.add(userCouponListInfo);
//        });


    }
}
